<template>
  <div class="reg">
    <u-form
      :model="form"
      ref="uForm"
      :error-type="errorType"
      label-width="120rpx"
      label-align="center"
    >
      <u-form-item prop="nickName" label="昵称"
        ><u-input v-model="form.nickName"
      /></u-form-item>
      <u-form-item prop="email" label="邮箱"
        ><u-input v-model="form.email"
      /></u-form-item>
      <u-form-item prop="passwd" label="密码"
        ><u-input v-model="form.passwd"
      /></u-form-item>
      <u-form-item label="头像"
        >
        <u-upload :action="action" :multiple="false" @on-uploaded="uploaded" :file-list="fileList" ></u-upload>
      </u-form-item>
    </u-form>
    <div class="btn_foot">
      <u-button  type="success" @click="regFn">提交注册</u-button>
    </div>
    <u-toast ref="uToast" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      errorType: ["toast"],
      form: {
        email: "",
        passwd: "",
        nickName: "",
        photoUrl:""
      },
      action: '',
      fileList: [],
      rules: {
        email: [
          {
            required: true,
            message: "邮箱不能为空",
            trigger: "blur"
          }
        ],
        passwd: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur"
          }
        ],
        nickName: [
          {
            required: true,
            message: "昵称不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  created() {
    console.log(Date.parse(new Date()));
    let dateTime = Date.parse(new Date())
    this.action = "http://39.106.224.223:8088/ac/upload/"+dateTime
  },
  methods: {
    uploaded(list,name){
      // this.fileList = []
     console.log(list);
     console.log(this.fileList);
     this.form.photoUrl = "http://39.106.224.223:8088"+list[0].response.data
    //  this.fileList = [{url:"http://39.106.224.223:8088"+list[0].response.data}]
    },
    regFn() {
      console.log(this.form.photoUrl);
      
      this.$refs["uForm"].validate(valid => {
        if (valid) {
          let that = this;
          uni.request({
            url: "http://39.106.224.223:8088/ac/api/reg",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            data: {
              //  code:res.code
              email: that.form.email,
              passwd: that.form.passwd,
              nickName:that.form.nickName,
              photoUrl: that.form.photoUrl
            },
            success: res => {
              if (res.data.code == 200) {
                this.$refs.uToast.show({
                  title: "注册成功",
                  type: "success",
                  url: "pages/login/index"
                });
                // uni.redirectTo({
                //   url: "../login/index"
                // });
              } else {
                this.$refs.uToast.show({
                  title: "注册失败",
                  type: "error"
                });
              }
            },
            fail(er) {
              console.log(er);
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="scss">
.reg {
  width: 100%;
  height: 100%;
  .btn_foot {
    padding: 20rpx 10rpx;
    button {
      margin: 20rpx 0;
    }
  }
}
</style>